<?php
/**
 * Created by PhpStorm.
 * @User: 王崇全
 * @Date: 2018/11/12 14:28
 */

namespace app\common\model;


use think\Model;

/**
 * Class Region
 * @author  王崇全
 * @date    2018/11/12 16:30
 * @package app\common\model
 */
class Region extends Model
{
    protected $name = 'regions';

    protected $config = [
        'cache' => 20140210,
        'field' => 'code,name',
    ];

    /**
     * 地区列表
     * @author 王崇全
     * @date   2018/11/12 14:36
     * @param int $code 父级地区代码 0为省级的父级
     * @param string $keywords
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function list(int $code = 0, string $keywords = "")
    {
        $code = str_pad($code, 6, 0);

        $db = $this->where('parent_id', $code)
                   ->field($this->config['field'])
                   ->cache($this->config['cache'])
                   ->order("code");
        if ($keywords) {

            $db->whereLike('name|initial|pinyin', '%'.$keywords.'%');
        }

        return $db->select();
    }

    /**
     * 检索
     * @author 王崇全
     * @date   2018/11/12 16:06
     * @param int    $level    1,省;2,市;3,县;4,乡
     * @param string $keywords 关键词
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function search(int $level, string $keywords = "")
    {
        if ((int)$level > 4 || (int)$level < 1) {
            return [];
        }

        $db = $this->where('level', $level)
                   ->field($this->config['field'])
                   ->cache($this->config['cache'])
                   ->order("code");
        if ($keywords) {

            $db->whereLike('name|initial|pinyin', '%'.$keywords.'%');
        }

        return $db->select();
    }

    /**
     * 省列表
     * @author 王崇全
     * @date   2018/11/12 14:37
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function getProvince()
    {
        return $this->where('level', 1)
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }

    /**
     * 市列表
     * @author 王崇全
     * @date   2018/11/12 14:37
     * @param int $provinceCode 省代码
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function getCity(int $provinceCode)
    {
        return $this->where('parent_id', $provinceCode)
                    ->where('level', 2)
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }

    /**
     * 县区列表
     * @author 王崇全
     * @date   2018/11/12 14:37
     * @param int $cityCode 市列表
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function getDistrict(int $cityCode)
    {
        return $this->where('parent_id', $cityCode)
                    ->where('level', 3)
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }

    /**
     * 乡镇街道列表
     * @author 王崇全
     * @date   2018/11/12 14:37
     * @param int $districtCode 县区代码
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function getStreet(int $districtCode)
    {
        return $this->where('parent_id', $districtCode)
                    ->where('level', 4)
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }

    /**
     * 下级地区检索
     * @author 王崇全
     * @date   2018/11/12 14:37
     * @param  string $keywords 关键词
     * @param int     $code     本级代码
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function searchRegion(string $keywords, $code = 0)
    {
        return $this->where('parent_id', $code)
                    ->whereLike('name|initial|pinyin', '%'.$keywords.'%')
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }

    /**
     * 省检索
     * @author 王崇全
     * @date   2018/11/12 14:37
     * @param   string $keywords 关键词
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function searchProvince(string $keywords)
    {
        return $this->where('level', 1)
                    ->whereLike('name|initial|pinyin', '%'.$keywords.'%')
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }

    /**
     * 市检索
     * @author 王崇全
     * @date   2018/11/12 14:38
     * @param string $keywords     关键词
     * @param int    $provinceCode 省代码
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function searchCity(string $keywords, $provinceCode = 0)
    {
        return $this->where('level', 2)
                    ->where('parent_id', $provinceCode)
                    ->whereLike('name|initial|pinyin', '%'.$keywords.'%')
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }

    /**
     * 县区检索
     * @author 王崇全
     * @date   2018/11/12 14:38
     * @param string $keywords 关键词
     * @param int    $cityCode 市代码
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function searchDistrict(string $keywords, $cityCode = 0)
    {
        return $this->where('level', 3)
                    ->where('parent_id', $cityCode)
                    ->whereLike('name|initial|pinyin', '%'.$keywords.'%')
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }

    /**
     * 乡镇街道检索
     * @author 王崇全
     * @date   2018/11/12 14:38
     * @param string $keywords     关键词
     * @param int    $districtCode 县区代码
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public function searchStreet(string $keywords, $districtCode = 0)
    {
        return $this->where('level', 4)
                    ->where('parent_id', $districtCode)
                    ->whereLike('name|initial|pinyin', '%'.$keywords.'%')
                    ->field($this->config['field'])
                    ->cache($this->config['cache'])
                    ->order("code")
                    ->select();
    }


    /**
     * 递归查询上级区域信息
     * @author 王海东
     * @date
     * @param int   $code   地区代码
     * @param array $arr    用于累计每次递归数据
     * @return array
     * @throws \Exception
     */
    public function showSuperior(int $code,$arr = [])
    :array {
        try {

            $data = $this->field([
                'code',
                'name',
                'level',
                'parent_id'
            ])
            ->where('code',$code)
            ->find();

            if($data['level'] != 1) {
                $arr = $this->showSuperior($data['parent_id'],$arr);
                array_push($arr,$data);
                return $arr;
            } else {
                array_push($arr,$data);
                return $arr;
            }

        } catch (\Exception $e) {
            throw new \Exception('查询失败');
        }
    }


}